/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Main.java
 *
 * Created on 2011-03-25, 20:34:51
 */
package frames;

import dialogs.DlgListCategories;
import entities.Year;
import panes.Month_Statistic;
import panes.Resume;
import utils.AutoSave;
import utils.ChocoDesign;
import utils.ChocoPane;
import utils.ClosablePane;

/**
 *
 * @author chocoseb
 */
public class Main extends javax.swing.JFrame {

    private static Year _year;
    private AutoSave _autoSave;

    /** Creates new form Main */
    public Main() {
        initComponents();
        this.setSize(1000, 800);
        statusBar.showInformationMessage("Ouverture de l'application");
        this.setTitle("ChocoBudjet V. 0.2.1");
        this.setLocationRelativeTo(null);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        statusBar = new utils.ChocoBar();
        tab = new javax.swing.JTabbedPane();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        mniNewYear = new javax.swing.JMenuItem();
        mniOpenBudjet = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JPopupMenu.Separator();
        mniExit = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        mnuOptions = new javax.swing.JMenu();
        mniCategories_Option = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        statusBar.setPreferredSize(new java.awt.Dimension(14, 30));
        getContentPane().add(statusBar, java.awt.BorderLayout.SOUTH);
        getContentPane().add(tab, java.awt.BorderLayout.CENTER);

        jMenu1.setText("File");

        mniNewYear.setText("New Budjet");
        mniNewYear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mniNewYearActionPerformed(evt);
            }
        });
        jMenu1.add(mniNewYear);

        mniOpenBudjet.setText("Open budjet...");
        mniOpenBudjet.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mniOpenBudjetActionPerformed(evt);
            }
        });
        jMenu1.add(mniOpenBudjet);
        jMenu1.add(jSeparator1);

        mniExit.setText("Exit");
        mniExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mniExitActionPerformed(evt);
            }
        });
        jMenu1.add(mniExit);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Edit");

        jMenuItem1.setText("Test");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem1);

        jMenuBar1.add(jMenu2);

        mnuOptions.setText("Options");

        mniCategories_Option.setText("Categories options");
        mniCategories_Option.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mniCategories_OptionActionPerformed(evt);
            }
        });
        mnuOptions.add(mniCategories_Option);

        jMenuBar1.add(mnuOptions);

        setJMenuBar(jMenuBar1);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void mniExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mniExitActionPerformed
        if (_year != null) {
            _year.save();
        }
        System.exit(0);
    }//GEN-LAST:event_mniExitActionPerformed

    private void mniNewYearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mniNewYearActionPerformed
        DlgNewYear dlg = new DlgNewYear(this, true);
        dlg.setVisible(true);
        if (dlg.isConfirm()) {
            _year = dlg.getNewYear();
            this.addPane(new Resume(this, statusBar, _year));
            startAutoSave();
        }
    }//GEN-LAST:event_mniNewYearActionPerformed

    private void mniOpenBudjetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mniOpenBudjetActionPerformed
        DlgOpenYear dlg = new DlgOpenYear(this, true);
        dlg.setVisible(true);
        if (dlg.isConfirm()) {
            _year = dlg.getOpenedYear();
            this.addPane(new Resume(this, statusBar, _year));
            startAutoSave();
        }
    }//GEN-LAST:event_mniOpenBudjetActionPerformed

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void mniCategories_OptionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mniCategories_OptionActionPerformed
        DlgListCategories dlg = new DlgListCategories(null, true);
        dlg.setVisible(true);
    }//GEN-LAST:event_mniCategories_OptionActionPerformed

    private void addPane(ChocoPane p_pane) {
        tab.addTab(p_pane.getTitle(), p_pane);
        tab.setTabComponentAt(tab.getTabCount() - 1, new ClosablePane(tab));
        tab.setSelectedIndex(tab.getTabCount() - 1);
    }

    public void showMonthStatitic() {
        this.addPane(new Month_Statistic(this, statusBar, _year.getSelectedMonthRef(), _year));
    }

    public void startAutoSave() {
        if (_autoSave != null) {
            _autoSave.stopSave();
        }
        _autoSave = new AutoSave(_year, statusBar);
        _autoSave.start();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                ChocoDesign.setLookToNimbus();
                new Main().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JPopupMenu.Separator jSeparator1;
    private javax.swing.JMenuItem mniCategories_Option;
    private javax.swing.JMenuItem mniExit;
    private javax.swing.JMenuItem mniNewYear;
    private javax.swing.JMenuItem mniOpenBudjet;
    private javax.swing.JMenu mnuOptions;
    private utils.ChocoBar statusBar;
    private javax.swing.JTabbedPane tab;
    // End of variables declaration//GEN-END:variables
}
